import CONFIG from '../utils/config'
import util from '../utils/util'
import { LOCAL } from '../config/storage'

const baseURL = CONFIG.baseURL
function showToast(message, duration) {
    return new Promise((resolve, reject) => {
      wx.showToast({
        title: message,
        duration: duration,
        icon: 'none',
        success: resolve,
        fail: reject
      });
    });
  }
  
  function printError({ url, data, e }) {
    console.log('=======接口报错=======');
    console.log('入参：', data);
    console.log('地址：', url);
    console.log('回参：', e);
    if (e.head.faultstring == "没有登录" ) {
      console.log('===========没有登录=======2222==================');
      showToast(e.head.faultstring == "没有登录" ? '请登录':'请登录', 2000)
        .then(() => {
          console.log('Waiting 1.2 seconds before redirecting...');
          wx.setStorageSync('accessToken', '');
          setTimeout(() => {
            console.log('pages/login/qrcode');
            // wx.redirectTo({
            //   url: '/pages/login/qrcode'
            // });
          }, 1200);
        })
        .catch(error => {
          console.error('Toast error:', error);
        });
    }
  
    console.log('=======END=======');
  }

const transeResponse = data => {
    let { version, body, head } = data
    if (version === '2.0') {
        body = [body]
    }
    return {
        version,
        body,
        head
    }
}

function formatData(data, head = false, version = false) {
    let headCop = {}
    if (head && head.maxresults) {
        headCop.bodytype = 'flat'
        headCop.maxresults = head.maxresults
        headCop.firstresult = head.firstresult * head.maxresults
    } else {
        headCop = {
            firstresult: 0,
            maxresults: 10,
            bodytype: 'flat'
        }
    }
    let body
    if (Array.isArray(data)) {
        body = data
    } else {
        body = [data]
    }
    // console.log(version)
    if (version && version == '2.0') {
        version = '2.0'
    } else {
        version = '1.0'
    }
    return {
        version,
        head: headCop,
        body
    }
}

function formatUrl(url) {
    if (url.substring(0, 5).includes('http')) {
        return url
    } else {
        return baseURL + url
    }
}

function formatMedia(url) {
    /*if (url) {
      return baseURL + url.replace(/\/api/, '')
    } else {
      return url
    }*/
    return url
}

function formatImage(url) {
    /*if (url) {
      return 'http://mirror.laikang.com' + url.replace(/\/api/, '')
    } else {
      return url
    }*/
    return url
}

function nodePost(url, data) {
    url = formatUrl(url)
    return new Promise((reslove, reject) => {
        wx.request({
            url,
            data,
            header: {
                Cookie: wx.getStorageSync('token') || ''
            },
            method: 'POST',
            success: res => {
                if (res.errMsg === 'request:ok') {
                    reslove(res.data)
                }
            },
            fail: e => {
                console.log(e, '请求失败')
                reject(e)
            }
        })
    })
}

function standardFormatData(
    data,
    head = false,
    version = false,
    resCodeData = {
        businessSerialNo: '20230613183123721115',
        organizationCode: 'LK000042',
        systemCode: 'SYS0035'
    }
) {
    const page = head.firstresult || 1
    const max = head.maxresults
    if (head) {
        head = {}
        head.bodytype = 'flat'
        head.maxresults = max || 10
        head.firstresult = (page - 1) * head.maxresults
    } else {
        head = {
            firstresult: 0,
            maxresults: 10,
            bodytype: 'flat'
        }
    }
    let body
    // console.log(data, '入参data')
    if (data.appendix) {
        const summary = JSON.parse(data.appendix).summary
        summary.lkdnBusinessScen.businessSerialNo = resCodeData.businessSerialNo
        summary.lkdnBusinessScen.organizationCode = resCodeData.organizationCode
        summary.lkdnBusinessScen.systemCode = resCodeData.systemCode
        data.appendix = JSON.stringify({ summary })
    } else if (data.lkdnheaders) {
        const lkdnheaders = JSON.parse(data.lkdnheaders)
        lkdnheaders.lkdnBusinessScen.businessSerialNo = resCodeData.businessSerialNo
        lkdnheaders.lkdnBusinessScen.organizationCode = resCodeData.organizationCode
        lkdnheaders.lkdnBusinessScen.systemCode = resCodeData.systemCode
        data.lkdnheaders = JSON.stringify(lkdnheaders)
    }
    if (Array.isArray(data)) {
        body = data
    } else {
        body = [data]
    }

    console.log(version)
    if (version && version == '2.0') {
        version = '2.0'
    } else {
        version = '1.0'
    }
    return {
        version,
        head,
        body
    }
}
function standardAppendix(
    data,
    head = false,
    version = false,
    resCodeData = {
        businessSerialNo: '20230613183123721115',
        organizationCode: 'LK000042',
        systemCode: 'SYS0035'
    }
) {
    const page = head.firstresult || 1
    const max = head.maxresults
    if (head) {
        head = {}
        head.bodytype = 'flat'
        head.maxresults = max || 10
        head.firstresult = (page - 1) * head.maxresults
    } else {
        head = {
            firstresult: 0,
            maxresults: 10,
            bodytype: 'flat'
        }
    }
    let body
    // console.log(data, '入参data')
    if (data.appendix) {
        const summary = JSON.parse(data.appendix).summary
        summary.lkdnBusinessScen.businessSerialNo = resCodeData.businessSerialNo
        summary.lkdnBusinessScen.organizationCode = resCodeData.organizationCode
        summary.lkdnBusinessScen.systemCode = resCodeData.systemCode
        data.appendix = JSON.stringify({ summary })
    }
    if (Array.isArray(data)) {
        body = data
    } else {
        body = [data]
    }

    console.log(version)
    if (version && version == '2.0') {
        version = '2.0'
    } else {
        version = '1.0'
    }
    return {
        version,
        data,
        head,
        body
    }
}

export default {
    baseURL,
    formatMedia,
    formatImage,
    formatUrl,
    pageInit(_data) {
        let that = this
        let data
        data = _data
        let oldPage = Page
        Page = function (obj) {
            let pageName
            let onpagesRoute
            let pagesOptions
            let prevPageRoute
            let startTime
            let endTime
            let oldOnShow = obj.onShow
            obj.onShow = function () {
                // 此处写进入页面逻辑
                startTime = +new Date()
                console.log('开始时间', startTime)
                let pages = getCurrentPages()
                if (pages.length > 0) {
                    console.log(pages.length - 1)
                    onpagesRoute = pages[pages.length - 1].route
                    pagesOptions = pages[pages.length - 1].options
                        ? pages[pages.length - 1].options
                        : ''
                    pageName = pages[pages.length - 1].pageName
                }
                if (pages.length > 1) {
                    prevPageRoute = pages[pages.length - 2].route
                }
                that.post('/api/stat/httpreq/page/path', {
                    data: {
                        path: onpagesRoute ? onpagesRoute : '',
                        referrer: prevPageRoute ? prevPageRoute : '',
                        name: pageName ? pageName : '',
                        params: pagesOptions ? JSON.stringify(pagesOptions) : ''
                    },
                    head: {
                        firstresult: 1,
                        maxresults: 10
                    }
                })
                    .then(res => {
                        console.log(res)
                    })
                    .catch(e => {
                        console.log(e)
                    })
                if (oldOnShow) {
                    // console.log('shenmeshihouzouzheli',this);
                    oldOnShow.call(this)
                }
            }
            // 重写onHide方法，用一个变量保存旧的onHide函数
            let oldOnHide = obj.onHide
            obj.onHide = function () {
                console.log(this.route)
                let pages = getCurrentPages()
                if (pages.length > 0) {
                    console.log(pages)
                }
                // 此处写离开页面逻辑
                endTime = +new Date()
                // console.log('开始时间1', startTime)
                // console.log('结束时间1', endTime)
                // console.log("页面停留时间1：" + (endTime - startTime))
                // console.log("path",onpagesRoute);
                // console.log("name",pageName);
                var stayTime = endTime - startTime
                that.post('/api/stat/httpreq/page/remain', {
                    data: {
                        path: onpagesRoute ? onpagesRoute : '',
                        referrer: prevPageRoute ? prevPageRoute : '',
                        remain: stayTime,
                        name: pageName ? pageName : '',
                        params: pagesOptions ? JSON.stringify(pagesOptions) : ''
                    },
                    head: {
                        firstresult: 1,
                        maxresults: 10
                    }
                })
                    .then(res => {
                        console.log(res)
                    })
                    .catch(e => {
                        console.log(e)
                    })
                if (oldOnHide) {
                    oldOnHide.call(this)
                }
            }
            let oldOnUnload = obj.onUnload
            obj.onUnload = function () {
                console.log(this.route)
                let pages = getCurrentPages()
                if (pages.length > 0) {
                    console.log(pages, '此处写离开页面逻辑')
                }
                // 此处写离开页面逻辑
                endTime = +new Date()
                var stayTime = endTime - startTime
                that.post('/api/stat/httpreq/page/remain', {
                    data: {
                        path: onpagesRoute ? onpagesRoute : '',
                        referrer: prevPageRoute ? prevPageRoute : '',
                        remain: stayTime,
                        name: pageName ? pageName : '',
                        params: pagesOptions ? JSON.stringify(pagesOptions) : ''
                    },
                    head: {
                        firstresult: 1,
                        maxresults: 10
                    }
                })
                    .then(res => {
                        console.log(res)
                    })
                    .catch(e => {
                        console.log(e)
                    })
                if (oldOnUnload) {
                    console.log(this)
                    oldOnUnload.call(this)
                }
            }
            return oldPage(obj)
        }
    },
    // 对参数进行格式化的get请求
    get(
        url,
        options = {
            data: {},
            head: {}
        },
        ishead = false
    ) {
        url = formatUrl(url)
        return new Promise((reslove, reject) => {
            const accessToken = wx.getStorageSync('accessToken')
            wx.request({
                url,
                data: options.data,
                header: {
                    Cookie: `${accessToken}`
                },
                success: res => {
                    let data = res.data
                    console.log(data);
                    if (res.statusCode !== 200) {
                        reject(data)
                    }
                    if (data.head) {
                        if (data.head.faultcode === 'ok') {
                            for (let i of data.body) {
                                i.logo = formatMedia(i.logo)
                            }
                            if (ishead) {
                                reslove({
                                    data: data.body,
                                    head: res.header
                                })
                            } else {
                                reslove(data.body)
                            }
                        } else {
                            console.error('接口报错', data)
                            console.log('接口地址：', url)
                            // printError({url, data: options, e: d})
                            reject(data.head)
                        }
                    }
                },
                fail: e => {
                    // printError({url, data: options, e})
                    reject(e)
                }
            })
        })
    },

    getNot(url, data) {
        return new Promise((resolve, reject) => {
            wx.request({
                url,
                data,
                success: res => {
                    let data = res.data
                    resolve(data)
                },
                fail: e => {
                    console.error('接口报错', data)
                    console.log('接口地址：', url)
                    reject(e)
                }
            })
        })
    },

    /**
     *
     * @param {*} url
     * @param {*} options {isCookie:请求 loginbyticket 不携带 cookie，其他请求携带 cookie}
     * @param {*} ishead
     * @returns
     */
    adminGet(
        url,
        options = {
            data: {},
            head: {},
            isCookie: true
        },
        ishead = false
    ) {
        url = formatUrl(url)
        const header = {}
        if (options.isCookie) {
            console.log(wx.getStorageSync('LKaccessToken'));
            header.Cookie = wx.getStorageSync('LKaccessToken')
        } else {
            header.Cookie = ''
        }
        return new Promise((reslove, reject) => {
            wx.request({
                url,
                data: options.data,
                header,
                success: res => {
                    let data = res.data
                    data = transeResponse(data)
                    if (res.statusCode !== 200) {
                        reject(data)
                    }
                    if (data.head) {
                        if (data.head.faultcode === 'ok') {
                            if (ishead) {
                                reslove({
                                    data: data.body,
                                    head: res.header
                                })
                            } else {
                                reslove(data.body)
                            }
                        } else {
                            console.error('接口报错', data)
                            console.log('接口地址：', url)
                            // printError({url, data: options, e: d})
                            reject(data.head)
                        }
                    }
                },
                fail: e => {
                    // printError({url, data: options, e})
                    reject(e)
                }
            })
        })
    },

    adminPost(
        url,
        options = {
            data: {},
            head: {},
            version: '1.0'
        },
        ishead = false
    ) {
        url = formatUrl(url)
        return new Promise((reslove, reject) => {
            wx.request({
                url,
                data: formatData(options.data, options.head, options.version),
                header: {
                    Cookie: wx.getStorageSync('LKaccessToken') || ''
                },
                method: 'POST',
                success: res => {
                    const d = res.data
                    if (res.statusCode !== 200) {
                        reject(d)
                    }
                    if (d.head) {
                        if (d.head.faultcode === 'ok') {
                            if (d.body) {
                                for (let i of d.body) {
                                    i.logo = formatMedia(i.logo)
                                }
                            }
                            if (ishead) {
                                reslove({
                                    data: d.body,
                                    head: res.header,
                                    xdpd: d.head,
                                    sell: d.head // 销售人员返回
                                })
                            } else if (!d.body) {
                                reslove(d.head.properties)
                            } else {
                                reslove(d.body)
                            }
                        } else {
                            printError({ url, data: options, e: d })
                            d.head.body = d.body
                            reject(d.head)
                        }
                    }
                },
                fail: e => {
                    printError({ url, data: options, e })
                    reject(e)
                }
            })
        })
    },

    // 对参数进行格式化的post请求
    post(
        url,
        options = {
            data: {},
            head: {},
            version: '1.0'
        },
        ishead = false
    ) {
        url = formatUrl(url)
        return new Promise((reslove, reject) => {
            const accessToken = wx.getStorageSync('accessToken')
            wx.request({
                url,
                data: formatData(options.data, options.head, options.version),
                header: {
                    Cookie: `${accessToken}`
                },
                method: 'POST',
                success: res => {

                    const d = transeResponse(res.data)

                    if (res.statusCode !== 200) {
                        reject(d)
                    }
                    if (d.head) {
                        if (d.head.faultcode === 'ok') {
                            // console.log(d)
                            if (d.body) {
                                for (let i of d.body) {
                                    i.logo = formatMedia(i.logo)
                                }
                            }
                            if (ishead) {
                                reslove({
                                    data: d.body,
                                    head: res.header,
                                    xdpd: d.head,
                                    sell: d.head // 销售人员返回
                                })
                            } else if (!d.body) {
                                reslove(d.head.properties)
                            } else {
                                reslove(d.body)
                            }
                        } else {
                            printError({ url, data: options, e: d })
                            d.head.body = d.body
                            reject(d.head)
                        }
                    }
                },
                fail: e => {
                    console.log(e)
                    printError({ url, data: options, e })
                    reject(e)
                }
            })
        })
    },
    // 对参数进行格式化的post请求
    postBcg(
        url,
        options = {
            data: {},
            head: {}
        },
        ishead = false
    ) {
        url = formatUrl(url)
        console.log(options.head)
        return new Promise((reslove, reject) => {
            wx.request({
                url,
                data: formatData(options.data, options.head),
                header: {
                    Cookie: wx.getStorageSync('token') || ''
                },
                method: 'POST',
                success: res => {
                    const data = res.data
                    if (res.statusCode !== 200) {
                        reject(data)
                    }
                    if (data.head) {
                        if (data.head.faultcode === 'ok') {
                            for (let i of data.body) {
                                i.logo = formatMedia(i.logo)
                            }
                            if (ishead) {
                                reslove({
                                    data: data.body,
                                    head: res.header,
                                    xdpd: data.head,
                                    sell: data.head // 销售人员返回
                                })
                            } else {
                                reslove({
                                    data: data.body,
                                    head: data.head
                                })
                            }
                        } else {
                            console.error('接口报错', data)
                            console.log('接口地址：', url)
                            reject(data.head)
                        }
                    }
                },
                fail: e => {
                    console.log(e)
                    reject(e)
                }
            })
        })
    },
    // 对参数未进行格式化的post请求
    postForm(url, data) {
        url = formatUrl(url)
        return new Promise((reslove, reject) => {
            wx.request({
                url,
                data: util.queryStringify(data),
                header: {
                    Cookie: wx.getStorageSync('token') || '',
                    'Content-Type': 'application/x-www-form-urlencoded'
                },
                method: 'POST',
                success: res => {
                    const d = res.data
                    //console.log(res)
                    if (res.statusCode !== 200) {
                        printError({ url, data, e: d })
                        reject(data)
                    }
                    if (d.head) {
                        if (d.head.faultcode === 'ok') {
                            reslove(d.body)
                        } else {
                            printError({ url, data, e: d })
                            reject(d.head)
                        }
                    }
                },
                fail: e => {
                    printError({ url, data, e })
                    reject(e)
                }
            })
        })
    },
    getBanner(position) {
        console.log(this)
        return this.post('/api/mallfrontpage/banner/' + CONFIG.orgCode, {
            data: {
                position
            }
        })
    },
    // 图片文件上传
    fileUpload(url, filePath, data = {}, name = 'file') {
        url = formatUrl(url)
        return new Promise((reslove, reject) => {
            wx.uploadFile({
                url,
                filePath: filePath,
                name,
                formData: data,
                header: {
                    Cookie: wx.getStorageSync('accessToken') || ''
                },
                success(res) {
                    const data = JSON.parse(res.data)
                    if (res.statusCode !== 200) {
                        reject(data)
                    }
                    if (data.head) {
                        if (data.head.faultcode === 'ok') {
                            reslove(data.body)
                        } else {
                            reject(data.head)
                        }
                    }
                },
                fail(e) {
                    reject(e)
                }
            })
        })
    },
    // 图片文件上传 进度条
    fileUploadProgress(
        that,
        url,
        filePath,
        upload_file_list,
        idx,
        data = {},
        name = 'file'
    ) {
        url = formatUrl(url)
        return new Promise((reslove, reject) => {
            //上传返回值
            const upload_task = wx.uploadFile({
                url,
                filePath: filePath,
                name,
                formData: data,
                header: {
                    Cookie: wx.getStorageSync('token') || ''
                },
                success(res) {
                    const data = JSON.parse(res.data)
                    if (res.statusCode !== 200) {
                        reject(data)
                    }
                    if (data.head) {
                        if (data.head.faultcode === 'ok') {
                            reslove(data.body)
                        } else {
                            reject(data.head)
                        }
                    }
                },
                fail(e) {
                    reject(e)
                }
            })
            //上传 进度方法
            upload_task.onProgressUpdate(res => {
                upload_file_list[idx]['upload_percent'] = res.progress
                that.setData({
                    'prove.imageArray': upload_file_list
                })
            })
        })
    },
    // 调用工具类函数-post
    ApiToolsPost(urls, value) {
        // if (typeof value === 'object') {
        //     value = JSON.stringify(value)
        // }
        // const options = {
        //     urls,
        //     method: 'POST',
        //     value,
        // }
        const url = `/lktoolsh5${urls}`
        try {
            return new Promise((reslove, reject) => {
                nodePost(url, value)
                    .then(res => {
                        // console.log(res,"成功的结果")
                        reslove(res)
                    })
                    .catch(e => {
                        console.log('渲染失败', e)
                        reject(e)
                    })
            })
        } catch (error) {
            console.log('Textlog error', error)
        }
    },
    // 返回cookie-get
    ApiCookieGet(urls, value) {
        const options = {
            data: {},
            head: {}
        }
        console.log(options)
        try {
            return new Promise((reslove, reject) => {
                this.get(urls,options,true)
                    .then(res => {
                        reslove(res)
                    })
                    .catch(e => {
                        reject(e)
                    })
            })
        } catch (error) {
            console.log('Textlog error', error)
        }
    },
    // 调用工具类函数-get
    ApiToolsGet(urls, value) {
        const options = {
            urls: `${urls}?${util.queryStringify(value)}`,
            method: 'GET',
            value: null
        }
        console.log(options)
        const url = '/api/nodetools/public/sendForward'
        try {
            return new Promise((reslove, reject) => {
                this.post(url, {
                    data: options
                })
                    .then(res => {
                        reslove(res[0].value)
                    })
                    .catch(e => {
                        reject(e)
                    })
            })
        } catch (error) {
            console.log('Textlog error', error)
        }
    },
    // 获取微信步数
    getWeRunData() {
        return new Promise((resolve, reject) => {
            wx.getWeRunData({
                success: res => {
                    const url = '/api/health/wxrundata/decodesave'
                    // const url = `/api/user/decodewarundata`
                    this.post(url, {
                        data: {
                            encrypteddata: res.encryptedData,
                            iv: res.iv
                        }
                    })
                        .then(data => {
                            let v = data[0].steps
                            // console.log('获取微信步数服务端解密成功', v)
                            resolve(v)
                        })
                        .catch(e => {
                            // console.error('获取微信步数服务端解密失败', e)
                            reject(e)
                        })
                },
                fail: e => {
                    if (e.errMsg.indexOf('getWeRunData:fail auth deny') > -1) {
                        e.code = 100001
                    }

                    console.error('获取微信步数失败', e)
                    reject(e)
                }
            })
        })
    },

    mock(response) {
        return new Promise(resolve => {
            resolve(response)
        })
    },
    // 数据标准化添加请求头字段
    dataStandardPost(
        url,
        options = {
            data: {},
            head: {},
            version: '1.0'
        },
        ishead = false
    ) {
        url = formatUrl(url)
        return new Promise((reslove, reject) => {
            this.post('/api/lkdn/businessscen/code').then(resCodeData => {
                wx.request({
                    url,
                    data: standardFormatData(
                        options.data,
                        options.head,
                        options.version,
                        resCodeData[0]
                    ),
                    header: {
                        Cookie: wx.getStorageSync('token') || ''
                    },
                    method: 'POST',
                    success: res => {
                        const d = res.data
                        if (res.statusCode !== 200) {
                            reject(d)
                        }
                        if (d.head) {
                            if (d.head.faultcode === 'ok') {
                                console.log(d)
                                if (d.body) {
                                    for (let i of d.body) {
                                        i.logo = formatMedia(i.logo)
                                    }
                                }
                                if (ishead) {
                                    reslove({
                                        data: d.body,
                                        head: res.header,
                                        xdpd: d.head,
                                        sell: d.head // 销售人员返回
                                    })
                                } else if (!d.body) {
                                    reslove(d.head.properties)
                                } else {
                                    reslove(d.body)
                                }
                            } else {
                                printError({ url, data: options, e: d })
                                d.head.body = d.body
                                reject(d.head)
                            }
                        }
                    },
                    fail: e => {
                        console.log(e)
                        printError({ url, data: options, e })
                        reject(e)
                    }
                })
            })
        })
    },
    // 数据标准化的 get 请求,在 Cookie 的同级 添加请求头信息
    dataStandardGet(
        url,
        options = {
            data: {},
            head: {}
        },
        ishead = false
    ) {
        url = formatUrl(url)
        return new Promise((reslove, reject) => {
            const accessToken = wx.getStorageSync('accessToken')
            wx.request({
                url,
                header: {
                    Authorization: `Bearer ${accessToken}` || '',
                    lkdnBusinessScensystemCode: 'SYS0035',
                    lkdnBusinessScenbusinessScenario: JSON.parse(
                        standardAppendix(options.data).data.appendix
                    ).summary.lkdnBusinessScen.businessScenario,
                    lkdnBusinessScenbusinessSubType: JSON.parse(
                        standardAppendix(options.data).data.appendix
                    ).summary.lkdnBusinessScen.businessSubType
                },
                success: res => {
                    let data = res.data
                    if (res.statusCode !== 200) {
                        reject(data)
                    }
                    if (data.head) {
                        if (data.head.faultcode === 'ok') {
                            for (let i of data.body) {
                                i.logo = formatMedia(i.logo)
                            }
                            if (ishead) {
                                reslove({
                                    data: data.body,
                                    head: res.header
                                })
                            } else {
                                reslove(data.body)
                            }
                        } else {
                            console.error('接口报错', data)
                            console.log('接口地址：', url)
                            // printError({url, data: options, e: d})
                            reject(data.head)
                        }
                    }
                },
                fail: e => {
                    // printError({url, data: options, e})
                    reject(e)
                }
            })
        })
    },
    // 对参数进行格式化的post请求
    post2(
        url,
        options = {
            data: {},
            head: {},
            version: '1.0'
        },
        ishead = false
    ) {
        url = formatUrl(url)
        return new Promise((reslove, reject) => {
            wx.request({
                url,
                data: formatData(options.data, options.head, options.version),
                header: {
                    Cookie: wx.getStorageSync('token') || ''
                },
                method: 'POST',
                success: res => {
                    console.log(res, 'res')
                    const d = res.data
                    console.log(d, 'ddd data 数据')

                    if (res.statusCode !== 200) {
                        reject(d)
                    }
                    if (d.head) {
                        if (d.head.faultcode === 'ok') {
                            console.log(d.body, 'ddd body 数据')
                            if (ishead) {
                                reslove({
                                    data: d.body,
                                    head: res.header,

                                })
                            } else if (!d.body) {
                                reslove(d.head.properties)
                            } else {
                                reslove(d.body)
                            }
                        } else {
                            printError({ url, data: options, e: d })
                            d.head.body = d.body
                            reject(d.head)
                        }
                    }
                },
                fail: e => {
                    console.log(e)
                    printError({ url, data: options, e })
                    reject(e)
                }
            })
        })
    },
}
